package com.swe.chums.dao.impl;

import com.swe.chums.dao.TagDao;
import com.swe.chums.dao.model.Tag;
import com.swe.chums.dao.model.TagPk;
import com.swe.chums.dao.exceptions.TagDaoException;
import java.util.List;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.transaction.annotation.Transactional;

public class TagDaoImpl extends AbstractDAO implements ParameterizedRowMapper<Tag>, TagDao
{
	protected SimpleJdbcTemplate jdbcTemplate;

	protected DataSource dataSource;

	/**
	 * Method 'setDataSource'
	 * 
	 * @param dataSource
	 */
	public void setDataSource(DataSource dataSource)
	{
		this.dataSource = dataSource;
		jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

	/**
	 * Method 'insert'
	 * 
	 * @param dto
	 * @return TagPk
	 */
	@Transactional
	public TagPk insert(Tag dto)
	{
		jdbcTemplate.update("INSERT INTO " + getTableName() + " ( web_resource_id, tag_type_id ) VALUES ( ?, ? )",dto.getWebResourceId(),dto.getTagTypeId());
		TagPk pk = new TagPk();
		pk.setId( jdbcTemplate.queryForInt("select last_insert_id()") );
		return pk;
	}

	/** 
	 * Updates a single row in the tag table.
	 */
	@Transactional
	public void update(TagPk pk, Tag dto) throws TagDaoException
	{
		jdbcTemplate.update("UPDATE " + getTableName() + " SET id = ?, web_resource_id = ?, tag_type_id = ? WHERE id = ?",dto.getId(),dto.getWebResourceId(),dto.getTagTypeId(),pk.getId());
	}

	/** 
	 * Deletes a single row in the tag table.
	 */
	@Transactional
	public void delete(TagPk pk) throws TagDaoException
	{
		jdbcTemplate.update("DELETE FROM " + getTableName() + " WHERE id = ?",pk.getId());
	}

	/**
	 * Method 'mapRow'
	 * 
	 * @param rs
	 * @param row
	 * @throws SQLException
	 * @return Tag
	 */
	public Tag mapRow(ResultSet rs, int row) throws SQLException
	{
		Tag dto = new Tag();
		dto.setId( rs.getInt( 1 ) );
		dto.setWebResourceId( rs.getInt( 2 ) );
		dto.setTagTypeId( rs.getInt( 3 ) );
		return dto;
	}

	/**
	 * Method 'getTableName'
	 * 
	 * @return String
	 */
	public String getTableName()
	{
		return "spring2013g3.tag";
	}

	/** 
	 * Returns all rows from the tag table that match the criteria 'id = :id'.
	 */
	@Transactional
	public Tag findByPrimaryKey(int id) throws TagDaoException
	{
		try {
			List<Tag> list = jdbcTemplate.query("SELECT id, web_resource_id, tag_type_id FROM " + getTableName() + " WHERE id = ?", this,id);
			return list.size() == 0 ? null : list.get(0);
		}
		catch (Exception e) {
			throw new TagDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the tag table that match the criteria ''.
	 */
	@Transactional
	public List<Tag> findAll() throws TagDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, web_resource_id, tag_type_id FROM " + getTableName() + " ORDER BY id", this);
		}
		catch (Exception e) {
			throw new TagDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the tag table that match the criteria 'tag_type_id = :tagTypeId'.
	 */
	@Transactional
	public List<Tag> findByTagType(int tagTypeId) throws TagDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, web_resource_id, tag_type_id FROM " + getTableName() + " WHERE tag_type_id = ?", this,tagTypeId);
		}
		catch (Exception e) {
			throw new TagDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the tag table that match the criteria 'web_resource_id = :webResourceId'.
	 */
	@Transactional
	public List<Tag> findByWebResource(int webResourceId) throws TagDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, web_resource_id, tag_type_id FROM " + getTableName() + " WHERE web_resource_id = ?", this,webResourceId);
		}
		catch (Exception e) {
			throw new TagDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the tag table that match the criteria 'id = :id'.
	 */
	@Transactional
	public List<Tag> findWhereIdEquals(int id) throws TagDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, web_resource_id, tag_type_id FROM " + getTableName() + " WHERE id = ? ORDER BY id", this,id);
		}
		catch (Exception e) {
			throw new TagDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the tag table that match the criteria 'web_resource_id = :webResourceId'.
	 */
	@Transactional
	public List<Tag> findWhereWebResourceIdEquals(int webResourceId) throws TagDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, web_resource_id, tag_type_id FROM " + getTableName() + " WHERE web_resource_id = ? ORDER BY web_resource_id", this,webResourceId);
		}
		catch (Exception e) {
			throw new TagDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the tag table that match the criteria 'tag_type_id = :tagTypeId'.
	 */
	@Transactional
	public List<Tag> findWhereTagTypeIdEquals(int tagTypeId) throws TagDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, web_resource_id, tag_type_id FROM " + getTableName() + " WHERE tag_type_id = ? ORDER BY tag_type_id", this,tagTypeId);
		}
		catch (Exception e) {
			throw new TagDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns the rows from the tag table that matches the specified primary-key value.
	 */
	public Tag findByPrimaryKey(TagPk pk) throws TagDaoException
	{
		return findByPrimaryKey( pk.getId() );
	}

}
